import i18n from '@/i18n'
import Vue from 'vue'

let vueI18n = new Vue({
  i18n
})

const station = {
  state: {
    checkRules: {
      'station_info.name': [
        // 请输入地球站名称
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_name'), trigger: 'blur' }
      ],
      'station_info.kind': [
        // 请选择地球站类型
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_kind'), trigger: 'blur' }
      ],
      'station_info.purpose': [
        // 请选择地球站用途
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_purpose'), trigger: 'blur' }
      ],
      'station_info.version': [
        // 请输入地球站版本号
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_version'), trigger: 'blur' }
      ],
      'addCuForm.device': [
        // 请输入终端编号
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_device'), trigger: 'blur' }
      ],
      'addCuForm.number': [
        // 请输入终端号码
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_number'), trigger: 'blur' }
      ],
      'addCuForm.unit_type': [
        // 请选择终端类型
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_unit_type'), trigger: 'change' }
      ],
      'addCuForm.modulation_type': [
        // 请选择调制方式
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_modulation_type'), trigger: 'change' }
      ],
      'addCuForm.fec_type': [
        // 请选择前向纠错编码
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_fec_type'), trigger: 'change' }
      ],
      'addCuForm.fec_code_rate': [
        // 请选择前向纠错编码率
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_fec_code_rate'), trigger: 'change' }
      ],
      'modifyCuForm.device': [
        // 请输入终端编号
        { required: true, message: vueI18n.$t('message.validate.devices.station.modifyCuForm_device'), trigger: 'blur' }
      ],
      'modifyCuForm.number': [
        // 请输入终端号码
        { required: true, message: vueI18n.$t('message.validate.devices.station.modifyCuForm_number'), trigger: 'blur' }
      ],
      'modifyCuForm.unit_type': [
        // 请选择终端类型
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_unit_type'), trigger: 'change' }
      ],
      'modifyCuForm.modulation_type': [
        // 请选择调制方式
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_modulation_type'), trigger: 'change' }
      ],
      'modifyCuForm.fec_type': [
        // 请选择前向纠错编码
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_fec_type'), trigger: 'change' }
      ],
      'modifyCuForm.fec_code_rate': [
        // 请选择前向纠错编码率
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_fec_code_rate'), trigger: 'change' }
      ]
    },
    checkRulesAccess: {
      'station_info.name': [
        // 请输入地球站名称
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_name'), trigger: 'blur' }
      ],
      'station_info.kind': [
        // 请选择地球站类型
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_kind'), trigger: 'blur' }
      ],
      'station_info.purpose': [
        // 请选择地球站用途
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_purpose'), trigger: 'blur' }
      ],
      'station_info.version': [
        // 请输入地球站版本号
        { required: true, message: vueI18n.$t('message.validate.devices.station.station_info_version'), trigger: 'blur' }
      ],
      'cu_info.device': [
        // 请输入终端编号
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_device'), trigger: 'blur' },
        { type: 'number', message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ],
      'cu_info.number': [
        // 请输入终端号码
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_number'), trigger: 'blur' },
        { type: 'number', message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ],
      'cu_info.unit_type': [
        // 请选择终端类型
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_unit_type'), trigger: 'change' }
      ],
      'cu_info.modulation_type': [
        // 请选择调制方式
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_modulation_type'), trigger: 'change' }
      ],
      'cu_info.fec_type': [
        // 请选择前向纠错编码
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_fec_type'), trigger: 'change' }
      ],
      'cu_info.fec_code_rate': [
        // 请选择前向纠错编码率
        { required: true, message: vueI18n.$t('message.validate.devices.station.addCuForm_fec_code_rate'), trigger: 'change' }
      ],
      'cu_info.speed_max': [
        // 最大速率
        { required: true, message: vueI18n.$t('message.validate.devices.station.accessStationCuForm_speed_max'), trigger: 'blur' },
        { type: 'number', message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ],
      'cu_info.power': [
        // 功率
        { required: true, message: vueI18n.$t('message.validate.devices.station.accessStationCuForm_power'), trigger: 'blur' },
        { type: 'number', message: vueI18n.$t('message.validate.common.number'), trigger: 'blur' }
      ],
      'cu_info.address': [
        // ip地址
        { required: true, message: vueI18n.$t('message.validate.devices.station.accessStationCuForm_address'), trigger: 'blur' }
      ]
    },

    stationList: [],
    stationMapLine: [],
    centerStation: null,

    openLayers: {
      changeStationState: [],
      changeCommunicationLine: null,
      changeOnLine: null,
      changeStationPosition: null
    }
  },
  mutations: {
    setStationList (state, data) {
      state.stationList = data
    },
    setStationState (state, data) {
      let station = state.stationList.find(item => item.id === data.id)
      if (station) {
        station.state = data.state
      }
    },
    // 将设置openlayers的station state的mutation独立出来
    setStationStateForOpenLayers (state, data) {
      let changeList = []
      for (let stationItem of data) {
        let station = state.stationList.find(item => item.id === stationItem.id)
        if (station) {
          changeList.push({
            key: `station_${stationItem.id}`,
            name: station.name || '',
            state: stationItem.state
          })
        }
      }
      state.openLayers.changeStationState = changeList
    },
    setCenterStation (state, data) {
      if (data.length !== 0) {
        state.centerStation = data[0]
      }
    },
    clearStationMapLine (state, data) {
      state.stationMapLine = []
    },
    clearChangeCommunicationLine (state, data) {
      state.openLayers.changeCommunicationLine = null
    },
    pushStationMapLine (state, data) {
      // 判断是否有重复
      // let ifRepeat = state.stationMapLine.find(item => item.caller_station_id === data.caller_station_id && item.called_station_id === data.called_station_id)
      // if (ifRepeat) return
      let callerStation = state.stationList.find(item => item.id === data.caller_station_id)
      let calledStation = state.stationList.find(item => item.id === data.called_station_id)
      if (callerStation && calledStation) {
        // 设置id
        let id = `communicationline_${data.caller_station_id}_${data.called_station_id}`
        state.stationMapLine.push({
          name: ``,
          id,
          caller_station_id: data.caller_station_id,
          called_station_id: data.called_station_id,
          coords: [
            [callerStation.longitude, callerStation.latitude],
            [calledStation.longitude, calledStation.latitude]
          ]
        })
        // openlayers
        state.openLayers.changeCommunicationLine = {
          operate: 'create',
          id,
          coords: [
            [callerStation.longitude, callerStation.latitude],
            [calledStation.longitude, calledStation.latitude]
          ]
        }
      }
    },
    deleteStationMapline (state, data) {
      for (let i in state.stationMapLine) {
        // if (state.stationMapLine[i].caller_station_id === data.caller_station_id && state.stationMapLine[i].called_station_id === data.called_station_id) {
        if (state.stationMapLine[i].caller_station_id === data.caller_station_id && state.stationMapLine[i].called_station_id === data.called_station_id) {
          state.stationMapLine.splice(i, 1)
          // openlayers
          // 设置id
          let id = `communicationline_${data.caller_station_id}_${data.called_station_id}`
          state.openLayers.changeCommunicationLine = {
            operate: 'delete',
            id,
            caller_station_id: data.caller_station_id,
            called_station_id: data.called_station_id
          }
          // openlayers
          state.openLayers.changeCommunicationLine = {
            operate: 'delete',
            id
          }
        }
      }
    },
    changeStationMapline (state, data) {
      let callerStation = state.stationList.find(item => item.id === data.caller_station_id)
      let calledStation = state.stationList.find(item => item.id === data.called_station_id)
      if (callerStation && calledStation) {
        // 设置id
        let id = `communicationline_${data.caller_station_id}_${data.called_station_id}`
        // openlayers
        state.openLayers.changeCommunicationLine = {
          operate: 'create',
          id,
          coords: [
            [callerStation.longitude, callerStation.latitude],
            [calledStation.longitude, calledStation.latitude]
          ]
        }
      }
    },
    changeStationCoords (state, data) {
      let station = state.stationList.find(item => item.id === data.station_id)
      if (station) {
        station.longitude = data.longitude
        station.latitude = data.latitude
      }
      state.openLayers.changeStationPosition = station
    },
    changeStationMapLineCoords (state, data) {
      for (let i in state.stationMapLine) {
        let lineData = state.stationMapLine[i]
        if (state.stationMapLine[i].caller_station_id === data.caller_station_id) {
          lineData.coords[0] = [data.longitude, data.latitude]
          state.stationMapLine.splice(i, 1)
          state.stationMapLine.push(lineData)
        } else if (state.stationMapLine[i].called_station_id === data.called_station_id) {
          lineData.coords[1] = [data.longitude, data.latitude]
          state.stationMapLine.splice(i, 1)
          state.stationMapLine.push(lineData)
        }
      }
    },
    // 入网的线数据
    pushNetworkAccessLine (state, data) {
      // 若没有中心站，则返回
      if (!state.centerStation) return
      // 设置id
      let id = `online_${data.id}`
      let station = state.stationList.find(item => item.id === data.id)
      if (!station) return
      if (station.state === 0) return
      setTimeout(() => {
        state.openLayers.changeOnLine = {
          operate: 'create',
          id,
          coords: [
            [station.longitude, station.latitude],
            [state.centerStation.longitude, state.centerStation.latitude]
          ]
        }
      }, 50)
    },
    deleteNetworkAccessLine (state, data) {
      // 设置id
      let id = `online_${data.id}`
      state.openLayers.changeOnLine = {
        operate: 'delete',
        id,
        station_id: data.id
      }
    }
  },
  actions: {
    async getStationList (context) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/devices/stations/'
      })
      context.commit('setStationList', response.data)
      // 设置入网的line
      let stationsState1 = response.data.filter(station => station.state === 1 || station.state === 2)
      for (let item of stationsState1) {
        if (!item.center) {
          setImmediate(() => {
            context.commit('pushNetworkAccessLine', item)
          })
        }
      }
    },
    async getCenterStation (context) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/abs/devices/stations/',
        params: {
          dept__center: 1
        }
      })
      context.commit('setCenterStation', response.data)
    },
    set530Event (context, payload) {
      let callerStation = context.state.stationList.find(item => item.id === payload.caller_station)
      let calledStation = context.state.stationList.find(item => item.id === payload.called_station)
      if (callerStation && calledStation) {
        context.commit('setStationState', {
          id: payload.caller_station,
          state: payload.caller_station_state
        })
        context.commit('setStationState', {
          id: payload.called_station,
          state: payload.called_station_state
        })
        context.commit('modifyUsingSubscribeFreqSegment', {
          ...payload,
          caller_station_longitude: callerStation.longitude,
          caller_station_latitude: callerStation.latitude,
          called_station_longitude: calledStation.longitude,
          called_station_latitude: calledStation.latitude
        })
      }
    },
    set535Event (context, payload) {
      context.commit('setStationState', {
        id: payload.caller_station,
        state: payload.caller_station_state
      })
      context.commit('setStationState', {
        id: payload.called_station,
        state: payload.called_station_state
      })
      context.commit('deleteUsingSubscribeFreqSegment', payload)
    },
    set536Event (context, payload) {}
  }
}
export default station
